package christ.mmd.bl;

import java.util.ArrayList;
import java.util.Date;

import android.os.Parcel;
import android.os.Parcelable;
import android.view.ViewGroup.MarginLayoutParams;

public class Event extends DataItem implements Parcelable {

	public enum Type implements Parcelable {
		None, GodOffice, ReadingBible, Prayer, Pilgrimage, Recollection, Lection, Masterclass, Sport, Party, Cinema, Tourism, Volunteer, Conference, Concert, Theatre, Game, Contest, Meeting, NightWatch, Discussion, Exhibition, Charity, Seminar;

		public static Type valueOfEx(String name) {
			Type result = None;
			if (name != null && name.trim().length() > 0) {
				result = Type.valueOf(name.trim());
			}
			return result;
		}

		@Override
		public int describeContents() {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(name());

		}

		public static final Parcelable.Creator<Type> CREATOR = new Parcelable.Creator<Type>() {
			public Type createFromParcel(Parcel in) {
				return Type.valueOfEx(in.readString());
			}

			public Type[] newArray(int size) {
				return new Type[size];
			}
		};
	};

	Date mDateBegin;
	Date mDateEnd;
	String mTitle;
	String mUrl;
	String mAddress;
	ArrayList<Long> mCommunityIdList;
	ArrayList<Region> mRegions;
	ArrayList<Type> mTypes;
	String mDescription;
	ArrayList<Image> mImages;

	public Event(long id, Date dateBegin, Date dateEnd, String title,
			String url, String address, String region, String type,
			String description) {
		this(id, dateBegin, dateEnd, title, url, address,
				new ArrayList<Long>(), region, type, description,
				new ArrayList<Image>());
	}

	public Event(long id, Date dateBegin, Date dateEnd, String title,
			String url, String address, ArrayList<Long> communityIdList,
			String region, String type, String description,
			ArrayList<Image> images) {
		super(id);
		mDateBegin = dateBegin;
		mDateEnd = dateEnd;
		mTitle = title;
		mUrl = url;
		mAddress = address;
		mCommunityIdList = communityIdList;

		mRegions = new ArrayList<Region>();
		String[] regionNames = region.split(",");
		for (String regionName : regionNames) {
			mRegions.add(Region.valueOfEx(regionName));
		}

		mTypes = new ArrayList<Type>();
		String[] typeNames = region.split(",");
		for (String typeName : typeNames) {
			mTypes.add(Type.valueOfEx(typeName));
		}

		mDescription = description;
		mImages = images;
	}

	public Event(Parcel in) {
		this(in.readLong(), new Date(in.readLong()), new Date(in.readLong()),
				in.readString(), in.readString(), in.readString(), in
						.readString(), in.readString(), in.readString());
		in.readList(mCommunityIdList, getClass().getClassLoader());
		in.readList(mImages, getClass().getClassLoader());
	}

	public Date getDateBegin() {
		return mDateBegin;
	}

	public ArrayList<Region> getRegions() {
		return mRegions;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		dest.writeLong(mId);
		dest.writeLong(mDateBegin.getTime());
		dest.writeLong(mDateEnd.getTime());
		dest.writeString(mTitle);
		dest.writeString(mUrl);
		dest.writeString(mAddress);

		StringBuilder regions = new StringBuilder();
		for (Region region : mRegions) {
			regions.append(region.name());
			if (mRegions.lastIndexOf(region) != mRegions.size() - 1) {
				regions.append(",");
			}
		}
		dest.writeString(regions.toString());

		StringBuilder types = new StringBuilder();
		for (Type type : mTypes) {
			types.append(type.name());
			if (mTypes.lastIndexOf(type) != mTypes.size() - 1) {
				types.append(",");
			}
		}
		dest.writeString(types.toString());
		dest.writeString(mDescription);
		dest.writeList(mCommunityIdList);
		dest.writeList(mImages);
	}

	public static final Parcelable.Creator<Event> CREATOR = new Parcelable.Creator<Event>() {
		public Event createFromParcel(Parcel in) {
			return new Event(in);
		}

		public Event[] newArray(int size) {
			return new Event[size];
		}
	};
}
